//
//  PersonViewController.m
//  BDD_TWO
//
//  Created by XTW on 2023/4/21.
//

#import "PersonViewController.h"
#import "TabBarView.h"
#import "UIView_extension.h"
#import "UIColor+Hexadecimal.h"
#import "PersonInfoViewController.h"
#import "SettingViewController.h"
#import "WFCUMessageListViewController.h"
#import <WFChatClient/WFCChatClient.h>
#import "ChooseFileViewController.h"
#import "HXPhotoPicker.h"

@interface PersonViewController () {
    TabBarView                 *tabBarView;
}
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UIView *unloginView;
@property (weak, nonatomic) IBOutlet UIView *loginView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;


@end

@implementation PersonViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tabBarView = [TabBarView sharedTabBarView];
    [self.navigationItem setHidesBackButton:YES];
    self.fd_prefersNavigationBarHidden = YES;
    [self initView];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:3];
    self.unloginView.hidden = [UserManger hasUserLogged];
    self.loginView.hidden = ![UserManger hasUserLogged];
    if ([UserManger hasUserLogged]) {
        [self requestUserContent];
    } else {
        self.headerImageView.image = [UIImage imageNamed:@"defaultHeader"];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self popGestureClose:self];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self popGestureOpen:self];
}

- (IBAction)headerPress {
    if (![UserManger hasUserLogged]) {
        [self loginPress];
        return;
    }
    PersonInfoViewController *viewCon = [[PersonInfoViewController alloc] initWithNibName:@"PersonInfoViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)audioPress {
    ChooseFileViewController *viewCon = [[ChooseFileViewController alloc] initWithNibName:@"ChooseFileViewController" bundle:nil];
    viewCon.type = 1;
    CustomNavigationCoutroller *navCon = [[CustomNavigationCoutroller alloc] init];
    navCon.viewControllers = @[viewCon];
    navCon.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.navigationController presentViewController:navCon animated:YES completion:nil];
}

- (IBAction)videoPress {
    HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypeVideo];
    HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:manager delegate:nil];
    [self presentViewController:nav animated:YES completion:nil];
}

- (IBAction)photoPress {
    HXPhotoManager *manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
    HXCustomNavigationController *nav = [[HXCustomNavigationController alloc] initWithManager:manager delegate:nil];
    [self presentViewController:nav animated:YES completion:nil];
}

- (IBAction)helpPress {
    if (![UserManger hasUserLogged]) {
        [self loginPress];
        return;
    }
    WFCUMessageListViewController *mvc = [[WFCUMessageListViewController alloc] init];
    WFCCConversation *object = [[WFCCConversation alloc] init];
    object.target = WFChatCustomer;
    mvc.conversation = object;
    mvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mvc animated:YES];
}

- (IBAction)settingPress {
    SettingViewController *viewCon = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    [self.navigationController pushViewController:viewCon animated:YES];
}

- (IBAction)loginPress {
    [[TabBarView sharedTabBarView] actionLogin];
}

#pragma mark ---刷新用户信息
- (void)requestUserContent {
    UserInfo *user = [UserManger currentLoggedInUser];
    if (user.avatar) {
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"defaultHeader"]];
    }
    self.nameLabel.text = user.userName;
    //用户个人信息
    [[ServiceRequest sharedService] GET:[NSString stringWithFormat:@"user/%@", [UserManger currentLoggedInUser].platformId] parameters:nil success:^(id responseObject) {
        UserInfo *user = [UserManger currentLoggedInUser];
        NSDictionary *contentDic = responseObject;
        user.avatar = [contentDic objectForKey:@"avatar"];
        user.userName = [contentDic objectForKey:@"nickName"];
        user.availableJinbi = [NSString stringWithFormat:@"%@", [contentDic objectForKey:@"balanceCoinAmount"]];
        [UserManger setUser:user];
    } failure:^(NSString *error, NSInteger code) {
        [self showHUDAlert:error];
    }];
}

- (void)initView {
    self.headerImageView.layer.borderWidth = 2.0f;
    self.headerImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.scrollView setFrame:CGRectMake(0, IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - IS_iPhoneX_Top - TabbarHeight - IS_iPhoneX_Bottom)];
//    [self.scrollView setContentSize:CGSizeMake(SCREEN_WIDTH_DEVICE, 606)];
    
}
@end
